
<template>
    <div class="content">
        <div>
            <label for="fileUpload">
                <div class="select-label">选择图片</div>
            </label>
            <input type="file" @change="fileChanged" id="fileUpload">
        </div>
        <div class="image-type-txt">
            <div class="drawing" v-if="imgType === 'drawing'">绘画</div>
            <div class="neutral" v-if="imgType === 'neutral'">中性</div>
            <div class="sexy" v-if="imgType === 'sexy'">性感</div>
            <div class="hentai" v-if="imgType === 'hentai'">变态</div>
            <div class="porn" v-if="imgType === 'porn'">色情</div>
            <div v-if="imgType === 'loading'">
                <img class="loading-image"
                    src=""
                    alt="">
            </div>
        </div>
    </div>
    <div class="file-name">{{ fileOrigin.name }}</div>
    <div class="image-content">
        <div class="image-tips">
            <span>图片显隐</span>
            <SW @changes="swChangeHandle" />
        </div>
        <div class="hide-img-content">
            <img :src="imgSrc" v-if="imgSrc && showImage">
            <div class="" v-if="!imgSrc">暂无图片</div>
            <div class="" v-if="imgSrc && !showImage">图片隐藏</div>
        </div>
    </div>

</template>
<script setup>
import { ref } from 'vue'
import SW from '@/components/Switch.vue'
import request from '../utils/request'
import { dataURLToBlob } from '@/utils/tool'
const imgType = ref(null)
const showImage = ref(false)
const imgSrc = ref('')
const fileOrigin = ref('')
const swChangeHandle = (e) => {
    showImage.value = e
}

const fileChanged = (ev) => {
    if (!ev.target.files.length) return
    let file = ev.target.files[0]
    if (!/image/.test(file.type)) {
        alert("文件不支持")
        return
    }

    // 文件大小限制4Mib
    let MBPass = file.size / 1024 / 1024 < 4
    if (!MBPass) {
        alert("文件大于4Mb")
        return
    }
    fileOrigin.value = file
    let form = new FormData()
    form.append('image', file)
    imgType.value = 'loading'
    imgSrc.value = null
    upload(form).then(async res => {
        imgType.value = Object.keys(res)[0]
        imgSrc.value = await getBlobUrl(file)
    })
}

const upload = (formFile) => {
    return request({
        url: '/check',
        method: 'post',
        data: formFile
    })
}

const getBlobUrl = (file) => {
    return new Promise((resolve, reject) => {
        let reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = function () {
            let blob = dataURLToBlob(reader.result)
            let url = URL.createObjectURL(blob)
            resolve(url)
        }
        reader.onerror = function (err) {
            reject(err);
        }
    })
}

</script>

<style>
* {
    user-select: none;
}

#fileUpload {
    display: none;
}

.select-label {
    width: 130px;
    height: 45px;
    line-height: 45px;
    text-align: center;
    background-color: lightslategray;
    cursor: pointer;
    border-radius: 3px;
    color: #fff;
}

.content {
    display: flex;
    justify-content: center;
    position: relative;
}

.file-name {
    text-align: center;
    margin: 20px auto 0 auto;
}

.image-content {
    text-align: center;
}

.image-type-txt {
    position: absolute;
    left: 55%;
    top: 10px;
    margin-left: 50px;
    font-size: 24px;
}

.image-type-txt div {
    display: inline;
    margin-right: 20px;
}

.hide-img-content div {
    margin: auto;
    width: min(80vw, 800px);
    height: 200px;
    background-color: antiquewhite;
    text-align: center;
    line-height: 200px;
    font-size: 30px;
}

.hide-img-content img {
    width: min(80vw, 800px);
}

.image-tips {
    display: inline-flex;
    align-items: center;
    margin: 20px auto;
}

.loading-image {
    width: 30px;
    height: 30px;
    animation: loading .8s linear infinite;
}

@keyframes loading {
    0% {
        transform: rotate(0deg);
    }

    100% {
        transform: rotate(360deg);
    }
}

.drawing,
.neutral {
    color: darkcyan;
}

.porn,
.hentai {
    color: red;
}

.sexy {
    color: orange;
}
</style>